
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipo.mm.web.formbeans;

import java.util.Locale;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
import org.ipo.mm.ejb.entities.City;
import org.ipo.mm.ejb.entities.Institution;
import org.ipo.mm.ejb.entities.InstitutionFamily;
import org.ipo.mm.ejb.entities.InstitutionRegion;
import org.ipo.mm.ejb.entities.InstitutionType;
import org.ipo.mm.web.util.ConstantManager;

/**
 *
 * @author makcro
 */
public class InstitutionBean extends org.apache.struts.validator.ValidatorForm {

    private String name;
    private String note;
    private String address;
    private String zip;
    private String telephone;
    private String fax;
    private String email;
    private String city;
    private String type;
    private String region;
    private String family;
    private String operation;
    private String web;

    public InstitutionBean() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = null;

        MessageResources mResources = (MessageResources) request.getAttribute(Globals.MESSAGES_KEY);
        Locale locale = (Locale) request.getSession().getAttribute(Globals.LOCALE_KEY);

        String create = mResources.getMessage(locale, "gen_create");
        String update = mResources.getMessage(locale, "gen_update");

        if (create.equals(getOperation()) || update.equals(operation)) {
            errors = super.validate(mapping, request);
        }



        return errors;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    

    /**
     * @return the note
     */
    public String getNote() {
        return note;
    }

    public String getWeb() {
        return web;
    }

    public void setWeb(String web) {
        this.web = web;
    }

    
  

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

   

    /**
     * @return the zip
     */
    public String getZip() {
        return zip;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    

    /**
     * @return the telephone
     */
    public String getTelephone() {
        return telephone;
    }

    /**
     * @param telephone the telephone to set
     */
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    /**
     * @return the fax
     */
    public String getFax() {
        return fax;
    }

    /**
     * @param fax the fax to set
     */
    public void setFax(String fax) {
        this.fax = fax;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    public String getOperation() {
        return operation;
    }

    /**
     * @param operation the operation to set
     */
    public void setOperation(String operation) {
        this.operation = operation;
    }

    @Override
    public void reset(ActionMapping mapping, HttpServletRequest request) {

        Boolean resetForm = (Boolean) request.getSession().getAttribute(ConstantManager.SESSION_INST_RESET_FORM);
        InstitutionType lastCreatedType = (InstitutionType) request.getAttribute(ConstantManager.REQUEST_TYPE_LAST_CREATED);
        InstitutionFamily lastCreatedFamily = (InstitutionFamily) request.getAttribute(ConstantManager.REQUEST_FAMILY_LAST_CREATED);
        InstitutionRegion lastCreatedRegion = (InstitutionRegion) request.getAttribute(ConstantManager.REQUEST_REGION_LAST_CREATED);
        City lastCreatedCity = (City) request.getAttribute(ConstantManager.REQUEST_CITY_LAST_CREATED);

        // automatically select newly created entities
        if(lastCreatedType != null){
            this.type = lastCreatedType.getId().toString();
        }
        if(lastCreatedCity != null){
            this.city = lastCreatedCity.getId().toString();
        }
        if(lastCreatedFamily != null){
            this.family = lastCreatedFamily.getId().toString();
        }
        if(lastCreatedRegion != null){
            this.region = lastCreatedRegion.getId().toString();
        }


        if (resetForm) {


            String action = request.getParameter("method");


            if ("showUpdateInstForm".equals(action) || "viewInstForm".equals(action)) {

                Institution inst = (Institution) request.getSession().getAttribute("dispatcher_showUpdateInstForm_inst");

                this.setAddress((inst.getAddress() == null)? "" : inst.getAddress());
                this.setCity(inst.getCity() == null ? "" : String.valueOf(inst.getCity().getId()));
                this.setEmail(inst.getEmail() == null ? "" : inst.getEmail());
                this.setFamily(String.valueOf(inst.getFamily().getId()));
                this.setFax(inst.getFax() == null ? "" : inst.getFax());
                this.setName(inst.getName() == null ? "" : inst.getName());
                this.setNote(inst.getNote() == null ? "" : inst.getNote());
                this.setRegion(inst.getRegion() == null ? "" : String.valueOf(inst.getRegion().getId()));
                this.setTelephone(inst.getTelephone() == null ? "" : inst.getTelephone());
                this.setType(inst.getType() == null ? "" : String.valueOf(inst.getType().getId()));
                this.setZip(inst.getZip() == null ? "" : inst.getZip());
                this.setWeb(inst.getWeb() == null ? "" : inst.getWeb());

            } else if ("showCreateInstForm".equals(action)) {
                this.setAddress(null);
                this.setCity(null);
                this.setEmail(null);
                this.setFamily(null);
                this.setFax(null);
                this.setName(null);
                this.setNote(null);
                this.setRegion(null);
                this.setTelephone(null);
                this.setType(null);
                this.setZip(null);
                this.setWeb(null);
            }

        }


    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getFamily() {
        return family;
    }

    public void setFamily(String family) {
        this.family = family;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}
